﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace CDT_CSDLNC
{
    public partial class ucDanhSachSanPham : UserControl
    {
        public ucDanhSachSanPham()
        {
            InitializeComponent();
        }

        public string maTinBan;

        private void btTimKiem_Click(object sender, EventArgs e)
        {
            dgvDanhSachSanPham.Rows.Clear();
            string tenSanPham = tbTenSanPham.Text;
            string loaiSanPham = cbLoaiSanPham.SelectedItem.ToString();
            DataTable dt = TinBanBUS.TimTinBanTheoTenSanPham(tenSanPham, loaiSanPham);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                dgvDanhSachSanPham.Rows.Add(i+1, row["MaTinBan"], row["TieuDeTinBan"], row["TenSanPham"],row["GiaBan"],row["TinhTrangTinBan"]);
            }
        }

        private void ucDanhSachSanPham_Load(object sender, EventArgs e)
        {
            cbLoaiSanPham.Items.Add("Loại 1");
            cbLoaiSanPham.Items.Add("Loại 2");
            cbLoaiSanPham.Items.Add("Loại 3");
            cbLoaiSanPham.SelectedIndex = 0;
        }

        private void XuLyKichBan()
        {
            frmMain main = (frmMain)this.ParentForm;
            int flag = main.flag;
            maTinBan = dgvDanhSachSanPham.SelectedRows[0].Cells["MaTB"].Value.ToString();
            switch (flag)
            {
                case 1:
                    main.kb1_buoc = 3;
                    main.KichBan1();
                    break;
                case 2:
                    main.kb2_buoc = 3;
                    main.KichBan2();
                    break;
                case 3:
                    main.kb3_buoc = 3;
                    main.KichBan3();
                    break;
                case 4:
                    main.kb4_buoc = 3;
                    main.KichBan4();
                    break;
            }
        }

        private void dgvDanhSachSanPham_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDanhSachSanPham.SelectedRows[0].Index >= 0)
            {
                XuLyKichBan();
            }
        }

    }
}
